<?php

/***  Documentation

    Greqo Gmail Mailer Class extends PHPMailer
    Last Update: $Date: 2009-05-21 04:48:45 +0000 (Thu, 21 May 2009) $
    Author: Tom at klenwell@gmail.com

DESCRIPTION
    An extension of the PHPMailer class that allows it to be used with PHPMailer
    without requiring alteration of the PHPMailer class itself.
    
    This class can be used independently of the Greqo library.  Make sure
    the PHPMAILER_BASEPATH path points to the correct location of the PhpMailer
    class. Note, however, that version 2 of PhpMailer supports Gmail and Php4.
    For details, see: http://phpmailer.codeworxtech.com/index.php?pg=changelog
  
USAGE
    $this->Mailer = new GmailMailer();
    $this->Mailer->Username = <Google login name>;
    $this->Mailer->Password = <Google password>;
    $this->Mailer->FromName = 'Greqo Test';
    $this->Mailer->AddAddress($this->Mailer->Username, $this->Mailer->Username);
    $this->Mailer->Subject = 'testing GmailMailer';
    $this->Mailer->Body = "This is a test of GmailMailer, an extension of PHPMailer for use with Gmail accounts.";
    if ( !$this->Mailer->send_email() ) print 'email failed';

REFERENCE
    PhpMailer Home: http://phpmailer.codeworxtech.com/index.php
    PhpMailer Elements: http://www.bblog.com/apidoc/elementindex_PHPMailer.html
    
NOTES
    the base class's method SmtpConnect is overridden below to enable SSL support.  
    This is necessary for usage with SMTP servers that require SSL/TLS 
    authentication, like GMAIL.  Modification based on this script: 
    
    http://www.buayacorp.com/archivos/phpmailer-con-gmail/
    
    note : SSL also requires SSL is enabled with PHP     
    
    You may get the following error, even when the mail is successfully sent :
    
        Warning: fgets() [function.fgets]: SSL: fatal protocol error in phpmailer\class.smtp.php on line 1024
        
    more info here: http://bugs.php.net/bug.php?id=23220
    
    This class is designed to be compatible with PHP 4.  Disable strict error
    notices in PHP 5 or above.
    
______________________________________________________________________________*/

// load base Greqo class
define('PHPMAILER_BASEPATH', dirname(dirname(__FILE__)) . '/lib/phpmailer/');
require_once(PHPMAILER_BASEPATH . 'class.phpmailer.php');

class GmailMailer extends PHPMailer
{
    // public
    // PhpMailer settings
    var $Mailer = 'smtp';               
    var $Port = 465;
    var $Host = 'ssl://smtp.gmail.com';
    var $Username = '';
    var $Password = '';
    var $FromName = '';
    var $From = '';
    var $Sender = '';
    var $Priority = 3;
    var $WordWrap = 75;
    var $language = 'en';
     
    // private
    var $_filename = '';
    var $_dirpath = '';


    /* MAGIC METHODS */
    // php4 constructor
    function GmailMailer()
    {
        $this->__construct();
        register_shutdown_function( array($this, '__destruct') );
    }


    // php5 constructor
    function __construct()
    {
        // set language
        if ( !empty($this->language) )
            $this->SetLanguage($this->language, PHPMAILER_BASEPATH . 'language/');
    }
    
    // destructor
    function __destruct() {}
  

    /* ** PUBLIC METHODS ** */
    function send_email()
    {
        $this->use_smtp();
        
        if ( $this->Send() )
            return 1;
        else
            trigger_error('unable to send message: ' . $this->ErrorInfo, E_USER_WARNING);
    }
    
    function use_smtp()
    {
        // sanity checks
        if ( empty($this->Host) ) { trigger_error('empty SMTP host', E_USER_WARNING); return 0; }
        if ( empty($this->Username) ) { trigger_error('empty SMTP login', E_USER_WARNING); return 0; }
        if ( empty($this->Password) ) { trigger_error('empty SMTP password', E_USER_WARNING); return 0; }
        
        // set SMTP properties
        $this->Mailer = 'smtp';
        $this->SMTPAuth = 1;
        
        // for windows: see http://us.php.net/mail
        if ( isset($_ENV['OS']) && strpos($_ENV['OS'], 'Win') !== FALSE ) $this->_ini_smtp();
        return;
    }
    
    function SmtpConnect()
    {
        if ( $this->smtp == NULL )
            $this->smtp = new SMTP();

        $this->smtp->do_debug = $this->SMTPDebug;
        $hosts = explode(";", $this->Host);
        $index = 0;
        $connection = ($this->smtp->Connected()); 

        // Retry while there is no connection
        while($index < count($hosts) && ! $connection)
        {
            // modification (source: http://www.buayacorp.com/archivos/phpmailer-con-gmail/)
            if ( preg_match ("# (([a-z] +: /)? [^:]+): (\ d+) #i", $hosts[$index], $match) )
            {
                $host = $match[1];
                $port = $match[3];
            }
            else
            {
                $host = $hosts[$index];
                $port = $this->Port;
            }
                    
            if ( $this->smtp->Connect($host, $port, $this->Timeout) )
            {
                if ( $this->Helo != '' )
                    $this->smtp->Hello($this->Helo);
                else
                    $this->smtp->Hello($this->ServerHostname());
            
                if ( $this->SMTPAuth )
                {
                    if ( !$this->smtp->Authenticate($this->Username, $this->Password) )
                    {
                        $this->SetError( $this->Lang("authenticate") );
                        $this->smtp->Reset();
                        $connection = 0;
                    }
                }
                $connection = 1;
            }
            $index++;
        }
            
        if ( !$connection ) $this->SetError( $this->Lang("connect_host") );
            
        return $connection;
    }


    /* ** PRIVATE METHODS ** */
    function _ini_smtp()
    {
        ini_set("SMTP", $this->Host);
        ini_set("sendmail_from", $this->From);
    }

} // end class

?>
